/*
 * MCAL_NVIC.c
 *
 *  Created on: Sep 24, 2025
 *      Author: ADMIN
 */


#include "MCAL_NVIC.h"

/************************************************************
 * This Function is used to transmit date in UASART NVIC
 ************************************************************/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (huart->Instance == RS485_UART_Type.uartHandle->Instance)
	{
		static uint8 RS485ReceiveData[600];
		RS485_UART_Type.ReceiveDateIndex = RS485ReceiveData;

		/* Receive data and preserve date into the static date arr */
		RS485ReceiveData[RS485_UART_Type.ReceiveByteCnt] = RS485_UART_Type.ReceiveByteArr;
		RS485_UART_Type.ReceiveByteCnt++;

		/* reflash the timer clock */
		RS485_UART_Timer.Start(&RS485_UART_Timer);
		RS485_UART_Timer.CleanCnt(&RS485_UART_Timer);

		HAL_UART_Receive_IT(RS485_UART_Type.uartHandle,(uint8_t *)&RS485_UART_Type.ReceiveByteArr,1);
	}

}


/************************************************************
 * This Function is used to transmit date in HWTimer NVIC
 * But HWTimer NVIC in the main.h
 * HAL_TIM_PeriodElapsedCallback
 ************************************************************/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 1 */
  if (htim->Instance == RS485_UART_Timer.timerhandle->Instance)
  {
    /* 1. Over time action */
    RS485_UART_OverTimeDeal_FuncV1();
    
    /* 2. clean the nvic flag */
    RS485_UART_Timer.Cleanflag(&RS485_UART_Timer);
    /* 3. Stop the timer */
    RS485_UART_Timer.Stop(&RS485_UART_Timer);
  }


  else if (htim->Instance == Boot_Status_Timer.timerhandle->Instance)
  {
    /* 1. Over time action */
    if (BSW_Boot_u8NowStatus == BOOT_STANDBY_STA)
    {
      
      BSW_Boot_u8NowStatus = BOOT_JUMPAPP_STA;
    }
    
    /* 2. clean the nvic flag */
    Boot_Status_Timer.Cleanflag(&Boot_Status_Timer);
    /* 3. Stop the timer */
    Boot_Status_Timer.Stop(&Boot_Status_Timer);    
  }


  else if(htim->Instance == Cycle_500ms_Timer.timerhandle->Instance)
  {
    /* 1. Over time action */
    if (Green_LED_Type.io_est == IO_LOW_PIN)
    {
      Green_LED_Type.Set_High(&Green_LED_Type);
    }
    else
    {
      Green_LED_Type.Set_Low(&Green_LED_Type);
    }

    if( BSW_Boot_u8NowStatus == BOOT_FLASHDEAL_STA)
    {
      if (Blue_LED_Type.io_est == IO_LOW_PIN)
      {
        Blue_LED_Type.Set_High(&Blue_LED_Type);
      }
      else
      {
        Blue_LED_Type.Set_Low(&Blue_LED_Type);
      }
    }
    /* 2. clean the nvic flag */
    Cycle_500ms_Timer.Cleanflag(&Cycle_500ms_Timer);  

  }
  /* USER CODE END Callback 1 */
}


/************************************************************
 * This Function is used to RS485 Over time Action
 ************************************************************/
void RS485_UART_OverTimeDeal_FuncV1(void)
{
  /* 1. Printf the date which from receive port */
  RS485_UART_Type.ReceiveByteLen = RS485_UART_Type.ReceiveByteCnt;
  RS485_UART_Type.ReceiveByteCnt = 0;

  /* 2.1 the received date is CMD Date */
  if (RS485_UART_Type.ReceiveByteLen == BOOT_CMD_LEN)
  {
    uint8 CMD_StayBoot[8] =  "StayBoot";
		uint8 CMD_OverBppt[8] =  "SendOver";

    if (compare_arrays(RS485_UART_Type.ReceiveDateIndex,CMD_StayBoot,BOOT_CMD_LEN) == 1)
    {
      if (BSW_Boot_u8NowStatus == BOOT_STANDBY_STA)
      {
        my_printf("Recieve CMD_StayBoot\r\n");
        BSW_Boot_u8NowStatus = BOOT_STAYBOOT_STA;
      }

      
    }

    else if (compare_arrays(RS485_UART_Type.ReceiveDateIndex,CMD_OverBppt,BOOT_CMD_LEN) == 1)
    {
      if (BSW_Boot_u8NowStatus == BOOT_STAYBOOT_STA)
      {
        my_printf("Recieve CMD_OverSend\r\n");
        BSW_Boot_u8NowStatus = BOOT_FLASHDEAL_STA;
      }
      
    }

    else
    {
      my_printf("Recieve ERR CMD\r\n");
    }
  }

  /* 2.2 the received date is Pocket Date */
  else if (RS485_UART_Type.ReceiveByteLen == BOOT_DATE_LEN)
  {
    /* Step1 : Status Check */
    if (BSW_Boot_u8NowStatus == BOOT_STAYBOOT_STA)
    {
      /* Step2 : CRC Check */
      if (CRCCheck (RS485_UART_Type.ReceiveDateIndex,RS485_UART_Type.ReceiveByteLen) == 1)
      {
        my_printf("Recieve PocketDate\r\n");
        BSW_Boot_u8NowStatus = BOOT_DATEDEAL_STA;
      }
      else
      {
        my_printf("<<<<<Recieve PocketDate but crc ERR\r\n");
      }
    }
    else
    {
      my_printf("<<<<<Recieve PocketDate but Status Not in StayBoot\r\n");
    }
  }

  /* 2.3 the received date is ERR Date */
  else
  {
    my_printf("Recieve ERR DATE\r\n");
  }

}


